package com.handy.plugin.auth.action;

import java.util.Date;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.handy.plugin.auth.bean.HAuthRole;
import com.handy.plugin.auth.bean.HAuthUser;
import com.handy.plugin.auth.dao.HAuthUserDAO;
import com.handy.util.CryptTool;
import com.handy.webwork.action.ActionForm;

/**
 * 接收用户名口令，返回验证结果。
 * @author rocken.zeng@gmail.com
 *
 */
public class UserCheckAction extends Action
{

	public String execute(){
		if (log.isDebugEnabled()){
			log.info("execute()");
		}
//		log.info("username = "+getUsername());
//		log.info("password = "+getPassword());
		setErrorFlag(0);//先设置登录失败
		setPassword(CryptTool.md5(getPassword()));
		user = validate(getUsername(), getPassword());
		// 用户不可用，重新登陆
		if (user != null) {
			setErrorFlag(1);
			getSession().setAttribute("user", user);
			//log.info("----"+((HAuthUser)getSession().getAttribute("user")).getUsername());
			
		}
		String ret = "";
		switch (getViewFormat()){
		case 1:
			ret = "html";
			break;
		case 2:
			ret = "xml";
			break;
		case 3:
			ret = "json";
			break;
		}
        return ret;
    }
	
	/**
	 * 登陆验证
	 * 
	 * @param username
	 *            用户名
	 * @param userpass
	 *            密码
	 * @return 用户对象，可能为空值
	 */
	public HAuthUser validate(String username,String userpass){
		HAuthUser user = new HAuthUserDAO().findByUsername(username);
		if (user != null){//是否有该用户
			if (!user.getPassword().equals(userpass)){//如果口令不对
				user=null;
			}else{
				switch (user.getState()){
				case 1://试用期间用户
					if (user.getEndtime()==null || user.getEndtime().before(new Date())){//如果试用结束时间为空或已到期
						user=null;
					}
					break;
				case 2://已注销用户，不可用
					user=null;
					break;
				}
			}
		}
		if(user != null){
			String roleids = "";
			for(HAuthRole hauthrole : user.getRoles()){
				if(hauthrole.getId()<=2) {
					if (user.getRolekind()!=2){//dev角色优先
						user.setRolekind(hauthrole.getId());
					}
				}
				if(roleids==""){
					roleids += hauthrole.getId();
				}else{
					roleids += "," + hauthrole.getId();
				}
				user.setRoleids(roleids);
			}
		}
		return user;
	}
	
	
	@SuppressWarnings("unchecked")
	public HashMap sendData() {
		HashMap root = new HashMap();
		root.put("errorflag", getErrorFlag());
		root.put("viewFormat", getViewFormat());
		return root;
	}
	

	
	public void getForm(ActionForm form) {
		if (null != form.getObject("username")){
			setUsername(form.getString("username"));
		}
		if (null != form.getObject("password")){
			setPassword(form.getString("password"));
		}
		
		if (null != form.getObject("view") && !"".equals(form.getObject("view"))){
			setViewFormat(form.getInteger("view"));
		}
	}
	
    private String username;
    private String password;
    private Integer errorFlag;
    
    /**
     * 返回视图的格式。<br>
     * 1.html输出 <br>
	 * 2.xml输出 <br>
	 * 3.json输出(默认输出) <br> 
     */
    private Integer viewFormat = 3; 
    
    private static final Log log = LogFactory.getLog(UserCheckAction.class);
    
    private HAuthUser user;

	public HAuthUser getUser() {
		return user;
	}
	public void setUser(HAuthUser user) {
		this.user = user;
	}
	public String getUsername()
	{
        return username;
    }
    public void setUsername(String username)
	{
        this.username = username;
    }

    public String getPassword()
	{
        return password;
    }
    public void setPassword(String password)
	{
        this.password = password;
    }
	
	public Integer getErrorFlag() {
		return errorFlag;
	}
	public void setErrorFlag(Integer errorFlag) {
		this.errorFlag = errorFlag;
	}

	public Integer getViewFormat() {
		return viewFormat;
	}

	public void setViewFormat(Integer viewFormat) {
		this.viewFormat = viewFormat;
	}

}
